using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 腐蚀Buff - 持续造成伤害并降低防御力
/// </summary>
public class CorrosionBuff : BuffBase
{
    private float m_TickInterval = 1.0f; // 每秒造成一次伤害
    private float m_LastTickTime;
    private long m_DamagePerTick;
    private float m_DefenseReduction;
    private float m_OriginalDefense;

    public CorrosionBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is CorrosionData corrosionData)
        {
            m_DamagePerTick = corrosionData.DamagePerTick;
            m_TickInterval = corrosionData.TickInterval;
            m_DefenseReduction = corrosionData.DefenseReduction;
        }
        else
        {
            m_DamagePerTick = 8;
            m_DefenseReduction = 0.2f; // 默认降低20%防御力
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        m_LastTickTime = 0f;
        
        // 保存原始防御力
        // if (Target.AttrComponent != null)
        // {
        //     m_OriginalDefense = Target.AttrComponent.Defense;
        //     Target.AttrComponent.SetDefense(m_OriginalDefense * (1.0f - m_DefenseReduction));
        // }
        
        // 播放腐蚀特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        Logger.Log("CorrosionBuff", $"{Target.name} 被腐蚀了，每秒造成 {m_DamagePerTick} 点伤害，防御力降低{m_DefenseReduction * 100}%");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        // 检查是否到了造成伤害的时间
        if (Time.time - m_LastTickTime >= m_TickInterval)
        {
            ApplyCorrosionDamage();
            m_LastTickTime = Time.time;
        }
    }

    private void ApplyCorrosionDamage()
    {
        if (Target != null && !Target.HealthComponent.IsDead)
        {
            // 造成腐蚀伤害
            Target.HealthComponent.TakeDamage(m_DamagePerTick, DamageType.Poison);
            
            Logger.Log("CorrosionBuff", $"{Target.name} 受到腐蚀伤害: {m_DamagePerTick}");
            
            // 触发伤害事件
            var args = ReferencePool.Acquire<EntityTakeDamageEventArgs>();
            args.Entity = Target;
            args.Damage = m_DamagePerTick;
            args.Attacker = Caster;
            EventHelper.Fire(this, args);
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("CorrosionBuff", $"{Target.name} 腐蚀效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始防御力
        // if (Target.AttrComponent != null)
        // {
        //     Target.AttrComponent.SetDefense(m_OriginalDefense);
        // }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Corrosion"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 腐蚀效果可以叠加，刷新持续时间
    }
}